/**
 * @projectName: project02bywdmtz
 * @package: com.cskaoyan.service
 * @className: WxTopicServiceImpl
 * @author: 冉夏
 * @description: TODO
 * @date: 2022/12/31 18:27
 * @version: 1.8
 */

package com.cskaoyan.service;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.bo.WxTopicDetailBo;
import com.cskaoyan.bean.bo.WxTopicGoodsDetailBo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxTopicVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
/*
*
* 这里的功能需要测试
* TODO
*
* */
@Service
public class WxTopicServiceImpl implements WxTopicService{

    @Autowired
    MarketTopicMapper topicMapper;
/*
* 获得所有的topic信息  且状态码是0的所有信息
* */
    @Override
    public CommonData<WxTopicVo> getAllTopInfo(BasePageInfo basePageInfo) {
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.createCriteria().andDeletedEqualTo(false);
        List<MarketTopic> topicList = topicMapper.selectByExample(marketTopicExample);
        LinkedList<WxTopicVo> wxTopicVos = new LinkedList<>();
        for (MarketTopic marketTopic : topicList) {
            WxTopicVo wxTopicVo = new WxTopicVo();
            BeanUtils.copyProperties(marketTopic,wxTopicVo);
            wxTopicVos.add(wxTopicVo);
        }
        //开启分页
        PageInfo<WxTopicVo> wxTopicVoPageInfo = new PageInfo<>(wxTopicVos);
        //获取分页信息
        int pages = wxTopicVoPageInfo.getPages();
        long total = wxTopicVoPageInfo.getTotal();
        CommonData<WxTopicVo> wxTopicVoCommonData = new CommonData<>();
        wxTopicVoCommonData.setTotal((int)total);
        wxTopicVoCommonData.setPage(pages);
        wxTopicVoCommonData.setLimit(basePageInfo.getLimit());
        wxTopicVoCommonData.setPage(basePageInfo.getPage());
        wxTopicVoCommonData.setList(wxTopicVos);
        return wxTopicVoCommonData;
    }
/*
* 获得当前主题下的详细商品信息 以及相关联商品信息
* 需要验证  TODO
* */
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Override
    public WxTopicDetailBo getTopicDetailInfo(Integer id) {
        //先查询当前传递过来id对应的主体信息
        MarketTopic targetTopic = topicMapper.selectByPrimaryKey(id);
        //获得当前主体下面的相关联的商品
        Integer[] relateGoodsId = targetTopic.getGoods();
        //通过当前相关联的商品的id查询商品的详细信息
        LinkedList<WxTopicGoodsDetailBo> TopicGoodsDetailBoList = new LinkedList<>();
        for (Integer integer : relateGoodsId) {
            WxTopicGoodsDetailBo topicGoodsDetailBo = new WxTopicGoodsDetailBo();
            //根据id查询商品
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(integer);
            //将当前商品的值赋值给目标对象
            BeanUtils.copyProperties(marketGoods,topicGoodsDetailBo);
            //将当前商品放入goods集合中
            TopicGoodsDetailBoList.add(topicGoodsDetailBo);
        }
        //循环结束得到当前主题下的商品的信息集合
        WxTopicDetailBo wxTopicDetailBo = new WxTopicDetailBo();
        wxTopicDetailBo.setTopic(targetTopic);
        wxTopicDetailBo.setGoods(TopicGoodsDetailBoList);
        return wxTopicDetailBo;
    }
/*
* 获得当前商品的推荐主题信息
* TODO  待测试
* */
    @Override
    public CommonData<MarketTopic> getRelatedTopic(Integer id) {
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.createCriteria().andDeletedEqualTo(false);
        //查询出所有的主题且没有删除的
        List<MarketTopic> marketTopics = topicMapper.selectByExample(marketTopicExample);
     /*   LinkedList<MarketTopic> targetTopicList = new LinkedList<>();
        //遍历当前的集合 拿到对应的属性
        for (MarketTopic marketTopic : marketTopics) {
            Integer[] goodsId = marketTopic.getGoods();
            for (Integer integer : goodsId) {
                //如果当前id在主体的id中 就将这个主题作为有效主题
                if(id==integer){
                    MarketTopic targetTopic = new MarketTopic();
                    //将有效主题放入到返回的集合中
                    targetTopicList.add(targetTopic);
                }
            }
        }*/
        //循环结束后得到目标集合
        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);
        long total = marketTopicPageInfo.getTotal();
        int pages = marketTopicPageInfo.getPages();
        int pageNum = marketTopicPageInfo.getPageNum();
        CommonData<MarketTopic> targetTopicCommonData = new CommonData<>();
        targetTopicCommonData.setTotal((int)total);
        targetTopicCommonData.setPages(pages);
        targetTopicCommonData.setPage(pageNum);
        targetTopicCommonData.setList(marketTopics);

        return targetTopicCommonData;
    }
}
